<?php
namespace app\common;

use yii;
/**
 * http请求类
 */
class HttpCurl {

	/**
	 * 发送http请求
	 * @param  string $url    http地址
	 * @param  array $params 提交参数
	 * @return string         http返回的结果
	 */
	public static function get($url, $params = null, $header = null) {
        $url = Yii::$app->params['yoyo']['url'].$url;
        $arr_header[] = "Content-Type:application/json";
        $arr_header[] = "Authorization:Basic ".base64_encode(Yii::$app->params['yoyo']['appkey'].':'.Yii::$app->params['yoyo']['appsecret']);

        $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
        $ch = curl_init();
        $opt = array(
            CURLOPT_URL             => $url,
            CURLOPT_POST            => 1,
            // CURLOPT_HEADER          => 0,
            // CURLOPT_BINARYTRANSFER  => 1,
            CURLOPT_POSTFIELDS      => json_encode($params),
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_TIMEOUT         => 10,
            CURLOPT_HTTPHEADER      => $arr_header,
            //CURLOPT_USERPWD         => $u ? $u : '',
        );
        if ($ssl)
        {
            $opt[CURLOPT_SSL_VERIFYHOST] = 2;
            $opt[CURLOPT_SSL_VERIFYPEER] = false;
        }
        curl_setopt_array($ch, $opt);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }


    public static function requestPost($url, $data, $ssl=true) {
        //curl完成
        $curl = curl_init();
        //设置curl选项
        curl_setopt($curl, CURLOPT_URL, $url);//URL
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '
    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0 FirePHP/0.7.4';
        curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);//user_agent，请求代理信息
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);//referer头，请求来源
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时时间
        //SSL相关
        if ($ssl) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//检查服务器SSL证书中是否存在一个公用名(common name)。
        }
        // 处理post相关选项
        curl_setopt($curl, CURLOPT_POST, true);// 是否为POST请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);// 处理请求数据
        // 处理响应结果
        curl_setopt($curl, CURLOPT_HEADER, false);//是否处理响应头
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//curl_exec()是否返回响应结果

        // 发出请求
        $response = curl_exec($curl);
        if (false === $response) {
            echo '<br>', curl_error($curl), '<br>';
            return false;
        }
        curl_close($curl);
        return $response;
    }

    public static function http($url, $method, $postfields=null, $cookie=null, $timeout=3, $headers=array()) {
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ci, CURLOPT_USERAGENT, 'curl'); //避免index.php >> is_mobile_request() Notice: Undefined index: HTTP_USER_AGENT
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ci, CURLOPT_ENCODING, "");

        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);

        curl_setopt($ci, CURLOPT_HEADER, false);

        if ($method == 'POST')
        {
            curl_setopt ( $ci, CURLOPT_POST, TRUE );
            if (! empty ( $postfields )) {
                curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
            }
        }

        if($cookie){
            curl_setopt ( $ci, CURLOPT_COOKIE, $cookie );
        }

        curl_setopt($ci, CURLOPT_URL, $url );
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
        curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );

        $response = curl_exec($ci);

        if($response === false)
        {
            echo curl_error($ci);
            exit;
        }
        curl_close ($ci);
//var_dump($response);exit;
        return json_decode($response,true);
    }
}
